#pragma once
#include <functional>
#include <iostream>
#include <string>
#include <map>

namespace MyTestMath
{
    
    const std::string masth_oper = "+-*/";

    class Task
    {
        using func_t = std::function<int(int, int, char)>;
    public:

        Task()
        {}
        
        Task(int x,int y, char operat, func_t func) 
            : _x(x)
            , _y(y)
            , _operation(operat)
            , __call_back__(func)
        {}

        std::string operator()()
        {
            int ret = __call_back__(_x, _y, _operation);
            char buffer[512];
            snprintf(buffer, sizeof(buffer), "%d %c %d = %d", _x, _operation, _y, ret);
            return buffer;
        }

        std::string toStringTask()
        {
            char buffer[512];
            snprintf(buffer, sizeof(buffer), "%d %c %d = ?", _x, _operation, _y);
            return buffer;
        }

    private:
        int _x;
        int _y;
        char _operation;
        func_t __call_back__;
    };

    int mymath(int a, int b, char opert)
    {
        std::map<char, std::function<int(int, int)>> oper_func = 
        {
            {'+', [](int x, int y) { return x + y; }},
            {'-', [](int x, int y) { return x - y; }},
            {'*', [](int x, int y) { return x * y; }},
            {'/', [](int x, int y) { 
                    if (y==0) 
                    {
                        std::cout << "除0错误" << std::endl;
                        return -1;
                    }
                    return x / y;
                }
            },
            {'%', [](int x, int y) { 
                    if (y==0) 
                    {
                        std::cout << "模0错误" << std::endl;
                        return -1;
                    }
                    return x % y;
                }
            }
        };
        return oper_func[opert](a, b);
    }
}